import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'
// const Manage = () => import('../views/Manage/index')
// const User = () => import('../views/User/index')
// const Home = () => import('../views/Home/index')
// const Permissions = () => import('../views/Permissions/index')
// const System = () => import('../views/System/index')
// const UpdatePassword = () => import('../views/UpdatePassword/index')
// const UserAdmin = () => import('../views/UserAdmin/index')
const login = () => import('../views/login/index')
const register = () => import('../views/register/index')
// const Person = () => import('../views/Person/index')
// const File = () => import('../views/File/index')
// const Role = () => import('../views/Role/index')
// const Menu = () => import('../views/Menu/index')
// const NotFound = () => import('../views/404.vue')
Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   name: 'Manage',
  //   component: () => import('../views/Manage/index'),
  //   redirect: '/Home',
  //   children: [
  //     {
  //       path: '/Home',
  //       name: '',
  //       component: () => import('../views/Home/index')
  //     },
  //     {
  //       path: '/User',
  //       name: '用户管理',
  //       component: () => import('../views/User/index')

  //     },
  //     {
  //       path: '/Permissions',
  //       name: '权限管理',
  //       component: () => import('../views/Permissions/index')
  //     },
  //     {
  //       path: '/System',
  //       name: '日志管理',
  //       component: () => import('../views/System/index')
  //     },
  //     {
  //       path: '/NotFound',
  //       name: '404页面',
  //       component: () => import('../views/404.vue')
  //     },
  // {
  //   path: '/UpdatePassword',
  //   name: '修改密码',
  //   component: () => import('../views/UpdatePassword/index')
  // },
  // {
  //   path: '/UserAdmin',
  //   name: '基本资料',
  //   component: () => import('../views/UserAdmin/index')
  // },
  //     {
  //       path: '/Person',
  //       name: '修改信息',
  //       component: () => import('../views/Person/index')
  //     },
  //     {
  //       path: '/File',
  //       name: '文件管理',
  //       component: () => import('../views/File/index')
  //     },
  //     {
  //       path: '/Role',
  //       name: '角色管理',
  //       component: () => import('../views/Role/index')
  //     },
  //     {
  //       path: '/Menu',
  //       name: '菜单管理',
  //       component: () => import('../views/Menu/index')
  //     },
  //   ]
  // },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus");
  if (storeMenus) {
    // 获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes('Manage')) {
      // 拼装动态路由
      const manageRoute = {
        path: '/', name: 'Manage', component: () => import('../views/Manage/index.vue'), redirect: "/Home", children: [
          { path: 'person', name: '修改信息', component: () => import('../views/Person/index.vue') },
        ]
      }
      const menus = JSON.parse(storeMenus)
      menus.forEach(item => {
        if (item.path) {  // 当且仅当path不为空的时候才去设置路由
          let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '/index.vue') }
          manageRoute.children.push(itemMenu)
        } else if (item.children.length) {
          item.children.forEach(item => {
            if (item.path) {
              let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '/index.vue') }
              manageRoute.children.push(itemMenu)
            }
          })
        }
      })
      // 动态添加到现在的路由对象中去
      router.addRoute(manageRoute)
    }
  }
}
// 重置我就再set一次路由
setRoutes()
// 路由守卫
router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称，为了在Header组件中去使用
  store.commit("setPath")  // 触发store的数据更新
  // 未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      next("/NotFound")
    } else {
      // 跳回登录页面
      next("/login")
    }
  }
  next()  // 放行路由
})

export default router
